package com.wlk.controller;

import com.wlk.pojo.Admins;
import com.wlk.service.AdminsService;
import com.wlk.utils.Result;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Resource
    private AdminsService adminsService;

    @GetMapping("/login")
    public String layoutPage() {
        return "/index.jsp";
    }
    @PostMapping("/login")
    public String loginPage(@RequestParam("phone") String phone, @RequestParam("password") String password, Model model, HttpSession session) {
        Result result = adminsService.login(phone, password);
        // 登录成功
        if (result.getCode() == 200) {
            Admins admins = (Admins) result.getData();
            session.setAttribute("admins", admins);
            return "redirect:layoutPage";
        } else {
            model.addAttribute("msg", result.getMessage());
            return "redirect:index";
        }
    }
}
